package com.eagle.scali.commoncenter.config;


import com.eagle.scali.commoncenter.interceptor.FeignContextInterceptor;
import com.eagle.scali.commoncenter.interceptor.RestTemplateContextInterceptor;
import feign.Feign;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.alibaba.sentinel.annotation.SentinelRestTemplate;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration
@EnableWebMvc
public class CommonConfiguration extends WebMvcConfigurerAdapter {
//	/**
//	 * 请求拦截器
//	 */
//	@Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new UserContextInterceptor());
//    }

    /**
     * 创建Feign请求拦截器，在发送请求前设置认证的用户上下文信息
     */
    @Bean
    @ConditionalOnClass(Feign.class)
    public FeignContextInterceptor feignTokenInterceptor() {
        return new FeignContextInterceptor();
    }

    /**
     * RestTemplate拦截器
     * @return
     */
    @LoadBalanced
    @Bean
	@SentinelRestTemplate
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getInterceptors().add(new RestTemplateContextInterceptor());
        return restTemplate;
    }


}
